package com.wjw.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.wjw.demo.DividerItemDecoration.VERTICAL_LIST;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.id_recycler)
    RecyclerView id_recycler;
    private List<String> list;
    private MyAdapter myAdapter;
    @BindView(R.id.add)
    Button add;
    @BindView(R.id.del)
    Button del;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //黄油刀注册
        ButterKnife.bind(this);
        //设置数据
        initData();
        //创建适配器
        myAdapter = new MyAdapter(this, list);
        //设置适配器
        id_recycler.setAdapter(myAdapter);

        //设置RecyclerView的布局管理
        /**
         * ListView布局
         */
        LinearLayoutManager layoutManager = new LinearLayoutManager(
                this, LinearLayoutManager.VERTICAL, false);
        id_recycler.setLayoutManager(layoutManager);

        /**
         * GridView布局
         */
//        id_recycler.setLayoutManager(new GridLayoutManager(this,3));

        /**
         *水平GridView布局
         */
//        id_recycler.setLayoutManager(new StaggeredGridLayoutManager(
//                3,StaggeredGridLayoutManager.HORIZONTAL));

        // 分割线
        id_recycler.addItemDecoration(new DividerItemDecoration(//这个类可以copy
                this,
                VERTICAL_LIST
        ));

        //条目动画
        id_recycler.setItemAnimator(new DefaultItemAnimator());

        //点击事件
        //add条目
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAdapter.addData(1);
            }
        });
        //del条目
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAdapter.delData(1);
            }
        });

        //item 点击事件
        myAdapter.setDianji(new Dianji() {
            //单击
            @Override
            public void onItemClick(View view, int pos) {
                Toast.makeText(MainActivity.this, "Click:" + pos, Toast.LENGTH_SHORT).show();
            }

            //长按
            @Override
            public void onItemLongClick(View view, int pos) {
                Toast.makeText(MainActivity.this, "Long_Click:" + pos, Toast.LENGTH_SHORT).show();
            }
        });
    }

    //数据
    private void initData() {
        list = new ArrayList<>();
        for (int i = 'A'; i < 'z'; i++) {
            list.add("" + (char) i);
        }
    }


}
